//
//  DestimationTableViewCell.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/9.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "DestimationTableViewCell.h"
#import "VCBigCollectionViewCell.h"
#import "MoreModel.h"


@interface DestimationTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *marklabel;
@property (nonatomic, strong) UIButton *ALLButton;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSIndexPath *cellIndexPaths;
@property (nonatomic, strong) MoreModel *moreModel;
@end

@implementation DestimationTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.marklabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.marklabel.backgroundColor = [UIColor colorWithRed:0.29 green:0.74 blue:0.8 alpha:1];
        [self addSubview:self.marklabel];
        self.title = [[UILabel alloc] initWithFrame:CGRectZero];
        self.title.font = [UIFont systemFontOfSize:14];
        self.title.textColor = [UIColor grayColor];
        [self addSubview:self.title];
        [self createCollectionView];
        self.ALLButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.ALLButton.frame = CGRectZero;
        [self.ALLButton setTitle:@"More" forState:UIControlStateNormal];
        [self.ALLButton addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.ALLButton];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.marklabel.frame = CGRectMake(5 * XWIDTH, 7 * XHIGHT, 15 * XWIDTH, 15 * XWIDTH);
    self.marklabel.layer.masksToBounds = YES;
    self.marklabel.layer.cornerRadius = 8*XWIDTH;
    self.title.frame = CGRectMake(self.marklabel.right + 20 * XWIDTH, 5 * XHIGHT, WIDTH / 2, 20 * XHIGHT);
    if (self.elementModel.more ==TRUE) {
        self.ALLButton.frame = CGRectMake(self.width - 60, 5 * XHIGHT, 50 * XWIDTH, 20 * XHIGHT);
    }
    self.collectionView.frame = CGRectMake(10, self.marklabel.bottom + 5 * XHIGHT, self.width - 20, self.height - self.marklabel.bottom - 5 * XHIGHT);
    // item的大小
    self.flowLayout.itemSize = CGSizeMake(self.collectionView.width / 2 - 5, self.collectionView.width / 2 - 5);
    
    // item的最小行间距
    _flowLayout.minimumLineSpacing = 10;
    
    // item的最小列间距
    _flowLayout.minimumInteritemSpacing = 10;

    
    
    
}

#pragma mark - 赋值
-(void)setElementModel:(VCelementModel *)elementModel {
    if (_elementModel != elementModel) {
        _elementModel = elementModel;
    }
    self.title.text = self.elementModel.title;
    

    [self.collectionView reloadData];
    
    
}

- (void)buttonDidPress:(UIButton *)sender {
    NSString *url = [NSString stringWithFormat:@"%@%@/", DestinationMoreURL, self.elementModel.index];
    [XYNetWorkTool getWithUrl:url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        self.moreModel = [[MoreModel alloc] initWithDataSource:result];
        self.pushMoreController(self.moreModel);
    } fail:^(NSError *error) {
        NSLog(@"more的数据请求错误%@", error);
    }];
}





#pragma mark - create CollectionView

- (void)createCollectionView {
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    // 滑动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
  

    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:250/255.0 green:246/255.0 blue:232/255.0 alpha:1];
    [self.collectionView registerNib:[UINib nibWithNibName:@"VCBigCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"VCBigCollectionViewCellIdentifier"];
    [self addSubview:self.collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.elementModel.elementDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cellIndexPaths = indexPath;
    VCBigCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VCBigCollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.dataModel = self.elementModel.elementDataArray[indexPath.item];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.pushDetailedVC(self.elementModel.elementDataArray[indexPath.item]);
}


                               
                               
                               

- (void)awakeFromNib {
    // Initialization code
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
